package com.car.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.io.FileUtils;

import com.spider.web.utils.DateUtil;

 

public class FileUtil {
	// 锁
	static ReadWriteLock lock = new ReentrantReadWriteLock();
	// 值
	double value = 0d;
	int addtimes = 0;
	public static String baseDir = "d:/sharedata";
	 
	public static void copyNio(File source, File target) {
		FileChannel ch1 = null;
		FileChannel ch2 = null;
		FileInputStream fin = null;
		FileOutputStream fout = null;
		try {

			fin = new FileInputStream(source);

			fout = new FileOutputStream(target);

			ByteBuffer buffer = ByteBuffer.allocate(512);

			ch1 = fin.getChannel();

			ch2 = fout.getChannel();

			while (ch1.read(buffer) != -1) { // 从通道中读取内容到缓冲区

				buffer.flip(); // 让缓冲区回到初始位置才能进行写操作

				ch2.write(buffer);

				buffer.clear(); // 清空缓冲区

			}

			ch1.close();

			ch2.close();

			fin.close();

			fout.close();

		}

		catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			try {
				ch1.close();
				ch2.close();

				fin.close();

				fout.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	/**
	 * 
	 * @param source
	 * @param targetDir
	 *            以地区，车牌号以文件名
	 */
	public static void writeNio(FileInputStream input, String targetDir) {
		FileChannel ch1 = null;

		FileChannel ch2 = null;
		FileOutputStream fout = null;
		try {
			// new File("d:\\efg.txt");
			File targetFile = new File(targetDir);
			fout = new FileOutputStream(targetFile);

			ByteBuffer buffer = ByteBuffer.allocate(512);

			ch1 = input.getChannel();

			ch2 = fout.getChannel();

			while (ch1.read(buffer) != -1) { // 从通道中读取内容到缓冲区

				buffer.flip(); // 让缓冲区回到初始位置才能进行写操作

				ch2.write(buffer);

				buffer.clear(); // 清空缓冲区

			}

		}

		catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			try {
				ch1.close();
				ch2.close();
				input.close();
				fout.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	/**
	 * 
	 * @param source
	 * @param targetDir
	 *            以地区，车牌号以文件名 未来可以实现断点续写
	 */
	public static void write(InputStream input, String targetDir) {
		Lock writeLock = lock.writeLock();
		writeLock.lock();
		System.out.println("ReadWriteLock  to addValue:     "
				+ System.currentTimeMillis());
		FileOutputStream out = null;

		try {
			// new File("d:\\efg.txt");
			File targetFile = new File(targetDir);
			out = new FileOutputStream(targetFile);
			int chByte = input.read();
			while (chByte != -1) { //
				out.write(chByte);
				chByte = input.read();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			try {
				input.close();
				out.flush();
				out.close();
				// 释放writeLock锁
				writeLock.unlock();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	public static String getTargetDir(final String dir) {
		// map.getcity map.getcarnumber
		String baseEnd = "/";
		if (baseDir.endsWith(baseEnd)) {
			baseEnd = "";
		}
		String dirPro = "/";
		if (dir.startsWith(dirPro)) {
			dirPro = "";
		}
		return baseDir + baseEnd
				+ DateUtil.convertFormatDate(DateUtil.getNowDate()) + dirPro
				+ dir;
	}

	
	private   static   int count=0;
	public static String lastDir="1";
	public static synchronized String getTargetDirTest() {
		// map.getcity map.getcarnumber
		String baseEnd = "/";
		if (baseDir.endsWith(baseEnd)) {
			baseEnd = "";
		}
	 
		 
		if(0==count%500){
			lastDir=count+"";
		}
		count++;
		return baseDir + baseEnd +  lastDir  ;
	}
	public static String getFileName(){
		return "NAME_"+count;
	}
	
	public static synchronized boolean saveFile(String fileContent){
		String dir=getTargetDirTest();
		newFolder(dir);
		String fileName=getFileName();
		File file=new File(dir+"/"+fileName+".html");
		//write(file,fileContent);
		try {
			write(  file,  fileContent) ;
			System.out.println("成功="+count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//FileUtils.writeStringToFile(file, data, encoding);
		return true;
	}
	/** 
     * 新建目录 
     * @param folderPath String 如 c:/fqf 
     * @return boolean 
     */ 
   public static void newFolder(String folderPath) { 
       try { 
           String filePath = folderPath; 
           filePath = filePath.toString(); 
           java.io.File myFilePath = new java.io.File(filePath); 
           if (!myFilePath.exists()) { 
               myFilePath.mkdir(); 
           } 
       } 
       catch (Exception e) { 
           System.out.println("新建目录操作出错"); 
           e.printStackTrace(); 
       } 
   }
   /** 
    * 删除文件夹 
    * @param filePathAndName String 文件夹路径及名称 如c:/fqf 
    * @param fileContent String 
    * @return boolean 
    */ 
  public static void delFolder(String folderPath) { 
      try { 
          delAllFile(folderPath); //删除完里面所有内容 
          String filePath = folderPath; 
          filePath = filePath.toString(); 
          java.io.File myFilePath = new java.io.File(filePath); 
          myFilePath.delete(); //删除空文件夹

      } 
      catch (Exception e) { 
          System.out.println("删除文件夹操作出错"); 
          e.printStackTrace();

      }

  }

  /** 
    * 删除文件夹里面的所有文件 
    * @param path String 文件夹路径 如 c:/fqf 
    */ 
  public static void delAllFile(String path) { 
      File file = new File(path); 
      if (!file.exists()) { 
          return; 
      } 
      if (!file.isDirectory()) { 
          return; 
      } 
      String[] tempList = file.list(); 
      File temp = null; 
      for (int i = 0; i < tempList.length; i++) { 
          if (path.endsWith(File.separator)) { 
              temp = new File(path + tempList[i]); 
          } 
          else { 
              temp = new File(path + File.separator + tempList[i]); 
          } 
          if (temp.isFile()) { 
              temp.delete(); 
          } 
          if (temp.isDirectory()) { 
              delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件 
              delFolder(path+"/"+ tempList[i]);//再删除空文件夹 
          } 
      } 
  }
  public static boolean write(File file,String str){
	  
	  FileWriter fw=null;
	try {
		fw = new FileWriter(file);
		fw.write(str);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
	}finally {
		try {
			fw.flush();
			fw.close();
		 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	  return true;
  }
  
   static int fileNameNumber=1;
   static String  baseDirError="c://error/";
	public static synchronized String getTargetDirError() {
		// map.getcity map.getcarnumber
		String filename="";
		fileNameNumber++;
		if(fileNameNumber<3000){
			filename="d:\\ones1\\"+fileNameNumber+".txt";
		}else if(fileNameNumber<6000){
			filename="d:\\ones2\\"+fileNameNumber+".txt";
		}else if(fileNameNumber<9000){
			filename="d:\\ones3\\"+fileNameNumber+".txt";
		}else if(fileNameNumber<12000){
			filename="d:\\ones4\\"+fileNameNumber+".txt";
		}else if(fileNameNumber<15000){
			filename="d:\\ones5\\"+fileNameNumber+".txt";
		}else{
			filename="d:\\ones6\\"+fileNameNumber+".txt";
		}
		System.out.println(fileNameNumber);
		return  filename;
	}
	
	public static String[] getUrls(){
		 
		Set pathSet=new HashSet();
		File file=new File("D:/error3/");
		if(file.isDirectory()){
			File[] files=file.listFiles();
			for(File f:files){
				if(f!=null){
					//System.out.println( f.getPath() );		
					pathSet.add(f.getPath()) ;
				}
			}
		}
		Object[] paths= pathSet.toArray();	
		System.out.println("file number:"+paths.length);
		Set<String> urls=new HashSet();
		for(Object url:paths){
			if(url!=null){
				
				urls.addAll( getLines(url.toString()) ) ;
			}		
		}
		System.out.println("urls number:"+urls.size());
		Set<String> rsUrls=new HashSet();
		String profile="https://www.cn.alibaba-inc.com/";
		for(String line:urls){
			if(line!=null){
				String url=line.substring( line.indexOf("C:")+2);
				if(url!=null){//https://www.cn.alibaba-inc.com
					if(url.startsWith("http")){						
					}else{
						url=profile+url;
					}
					rsUrls.add(url);
					//System.out.println( url );
				}
				
			}
			
		}
		System.out.println("return urls number:"+rsUrls.size());
		for(String url:rsUrls){
			//System.out.println(" url :"+url);
		}
		String[] arrayString=new String[]{};
		return rsUrls.toArray( arrayString );
	}
	public static List<String> getLines( String fileName )  

	{  

	    List<String> lines = new ArrayList<String>();  

	    try  

	    {  

	        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GB2312"));  

	        String line = null;  

	        while( ( line = br.readLine() ) != null )  
	        	lines.add(line);  
	        
	        br.close();  

	    }  

	    catch( FileNotFoundException e )  

	    {  

	    }  

	    catch( IOException e )  

	    {  

	    }  

	    return lines;  

	}  
	public static void main(String[] args) {

		//  copyNio(new File("c:\\abc.txt"), new File("d:\\efg.txt"));
		//newFolder("c:\\abc.txt");
		String[] arrayString=getUrls();
		System.out.println(arrayString.length);
		
	}
}
